<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('通知列表') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('标题')" prop="title">
        <el-input
          v-model="queryParams.title"
          :placeholder="$t('标题')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('发布时间')" prop="releaseTime">
        <DatePicker v-model="releaseTimes" type="daterange" />
      </el-form-item>
      <el-form-item :label="$t('发布人')" prop="releaseUser">
        <UserSelect v-model="queryParams.releaseUser" :placeholder="$t('发布人')" />
      </el-form-item>
      <el-form-item :label="$t('通知方式')" prop="methodInforme">
        <el-select v-model="queryParams.methodInforme" clearable :placeholder="$t('请选择')">
          <el-option
            v-for="(dict, index) in methodInformeOptions"
            :key="index"
            :value="dict.dictValue"
            :label="$t(dict.dictLabel)"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('是否通知全员')" prop="informAll">
        <el-select v-model="queryParams.informAll" clearable :placeholder="$t('请选择')">
          <el-option
            v-for="(dict, index) in yesNoOptions"
            :key="index"
            :value="dict.dictValue"
            :label="$t(dict.dictLabel)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['msg:send:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="msgSendList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('标题')" align="center" prop="title" />
      <el-table-column :label="$t('发送人')" align="center" prop="releaseUserName" />
      <el-table-column :label="$t('发送时间')" align="center" prop="releaseTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.releaseTime,'{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('通知方式')" align="center" prop="methodInforme" :formatter="methodInformeFormat" />
      <el-table-column :label="$t('通知状态')" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column :label="$t('系统通知-查看情况')" align="center" prop="msgTotal">
        <template slot-scope="scope">
          <el-link v-if="scope.row.msgTotal > 0" type="primary" @click="findHandle(scope.row.id)">
            已读&nbsp;{{ scope.row.haveRead }} &nbsp;&nbsp;/&nbsp;&nbsp;发送&nbsp;{{ scope.row.msgTotal }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['msg:send:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('详情') }}
          </el-button>
          <el-button
            v-hasPermi="['msg:send:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 编辑或修改【通知】对话框 -->
    <el-dialog
      :title="$t(title)"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
    >
      <msgSendForm ref="msgSendForm" />
    </el-dialog>
    <!-- 系统通知-查看情况 -->
    <el-dialog
      :title="$t('系统通知-查看情况')"
      :visible.sync="situationOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
      width="50%"
    >
      <msgSendSituation ref="msgSendSituation" />
    </el-dialog>

  </div>
</template>

<script>
import { listMsgSend, delMsgSend } from '@/api/system/message/msgSend'
import msgSendForm from './msgSendForm.vue'
import msgSendSituation from './miniView/msgSendSituation.vue'
export default {
  components: {
    msgSendForm,
    msgSendSituation
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      sendSituationLoading: false,
      // 总条数
      total: 0,
      // 【通知】表格数据
      msgSendList: [],
      // 弹出层标题
      title: '编辑通知',
      // 是否显示弹出层
      formOpen: false,
      situationOpen: false,
      // 是否 翻译Y/N 字典 sys_yes_no字典
      informAllOptions: [],
      // 是否 y/n 按钮
      yesNoOptions: [],
      // 岗位 字典
      receivePostOptions: [],
      // 通知方式 字典
      methodInformeOptions: [],
      // 发布时间 参数
      releaseTimes: [],
      // 通知状态 字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        user: {
          userId: undefined
        },
        title: undefined,
        content: undefined,
        releaseTime: undefined,
        releaseUser: undefined,
        releaseUserName: undefined,
        methodInforme: [],
        informAll: undefined,
        receiveRole: [],
        receivePost: [],
        receiveDept: [],
        receiveUser: [],
        nickName: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
    // 是否 字典sys_yes_no字典翻译
    this.getDicts('sys_yes_no').then(response => {
      this.informAllOptions = response.data
      this.yesNoOptions = response.data
    })
    // 岗位 字典interview_gw字典翻译
    this.getDicts('interview_gw').then(response => {
      this.receivePostOptions = response.data
    })
    // 通知方式 字典
    this.getDicts('msg_send_type').then(response => {
      this.methodInformeOptions = response.data
    })
    // 通知方式 字典
    this.getDicts('msg_send_status').then(response => {
      this.statusOptions = response.data
    })
  },
  methods: {
    // 查询通知列表
    getList() {
      this.loading = true
      // 时间参数赋值
      if (this.releaseTimes != null && this.releaseTimes.length > 1) {
        this.queryParams.startDate = this.releaseTimes[0]
        this.queryParams.endDate = this.releaseTimes[1]
      } else {
        this.queryParams.startDate = undefined
        this.queryParams.endDate = undefined
      }
      listMsgSend(this.queryParams).then(response => {
        this.msgSendList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    // 表单重置
    reset() {
      this.queryForm = {}
      this.resetForm('queryForm')
    },
    // 搜索按钮操作
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮操作
    resetQuery() {
      this.releaseTimes = []
      this.resetForm('queryForm')
    },
    // 新增按钮操作
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.msgSendForm.init(null)
      })
    },
    // 修改按钮操作
    updateHandle(rows) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.msgSendForm.init(rows)
      })
    },
    // 系统通知-查看情况
    findHandle(id) {
      this.situationOpen = true
      this.$nextTick(() => {
        this.$refs.msgSendSituation.findHandle(id)
      })
    },
    // 删除按钮操作
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delMsgSend(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    // 是否结束 字典 sys_yes_no字典翻译
    informAllFormat(row) {
      return this.selectDictLabel(this.informAllOptions, row.informAll)
    },
    // 岗位 字典 sys_yes_no字典翻译
    receivePostFormat(row) {
      return this.selectDictLabel(this.receivePostOptions, row.receivePost)
    },
    // 通知方式字典翻译
    methodInformeFormat(row) {
      return this.selectDictLabels(this.methodInformeOptions, row.methodInforme)
    },
    // 通知状态字典翻译
    statusFormat(row) {
      return this.selectDictLabels(this.statusOptions, row.status)
    }
  }
}
</script>
